package com.gxuwz.medical.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.user.User;
import com.gxuwz.medical.exception.DbException;
/**
 * 用户数据访问Dao
 * @author k88
 *
 */
public class UserDao extends BaseDao {

	@Override
	protected User handle(ResultSet rs) throws SQLException {
	    User user=new User();
		if(rs!=null){
	    	user.setUserid(rs.getString("userid"));
	    	user.setFullname(rs.getString("fullname"));
	    	user.setPwd(rs.getString("pwd"));
	    }
		return user;
	}
	/**
	 * 
	 * @param sql
	 * @param params
	 * @return
	 * @throws SQLException
	 */
	public User queryObject(String userid)throws DbException{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs =null;
		try{
			String sql="select * from t_user where userid=?";
			conn =DbUtil.getConn(); 
			pstmt=conn.prepareStatement(sql);
			int index=1;
			pstmt.setString(index, userid);
			rs=pstmt.executeQuery();
			User user=handle(rs);	
			return user;
		}catch (SQLException e) {
			throw new DbException("读取编号["+userid+"]用户信息失败", e);
		}
		
	}
	/**
	 * 
	 * @param sql
	 * @param page
	 * @param size
	 * @return
	 * @throws DbException
	 */
	public List<User> queryObjects(String sql,int page,int size)throws DbException{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs =null;
		List<User> results=new ArrayList<User>();
		try{
			
			conn =DbUtil.getConn(); 
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			do{
				User user=handle(rs);
				results.add(user);
			}while(rs.next());
				
			return results;
		}catch (SQLException e) {
			throw new DbException("读取用户信息分页信息失败", e);
		}
	}
	
	

}
